/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import Utils.DBConnection;
import Utils.Settings;
import Utils.Utils;
import com.mysql.jdbc.Util;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author user
 */
public class CustomerModel {

    DBConnection db = new DBConnection(Settings.getDb(), Settings.getDbLogin(), Settings.getDbPassword());
    String message;

    public HashMap ValidateUser(String username, String password) {
        try {
            String querry = "SELECT * "
                    + "FROM Customers "
                    + "WHERE Username =\"" + username + "\" AND Password =\"" + Utils.md5(password) + "\" ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);

            if (Rows.size() == 0) {
                message = "Username sau parola nu sunt corecte!";
                return null;
            }


            return (HashMap) Rows.get(0);
        } catch (Exception ex) {
            return null;
        } finally {
            //     db.close();
        }
    }

    public int GetCustomers_Count() {
        try {
            String query = "SELECT COUNT(*)  AS cnt FROM customers";
            ResultSet rs = db.ExecuteQuery(query);

            if (!rs.next()) {
                return -1;
            }
            return rs.getInt("cnt");
        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public ArrayList GetCustomers(int offset, int pageSize, String sort, String sortDir) {
        try {
            String querry = "SELECT * from Customers"
                    + " order by " + sort + " " + sortDir + " "
                    + " LIMIT " + offset + ", " + pageSize + " ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return Rows;
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public HashMap GetCustomer(int customerID) {
        try {
            String querry = "SELECT * from Customers"
                    + " WHERE CustomerID=" + customerID;

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return (HashMap) Rows.get(0);
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public int DeleteCustomer(int customerID) {
        try {

            String query = "DELETE FROM Customers "
                    + " WHERE CustomerID=" + customerID + " ";

            int rows = db.ExecuteUpdate(query);

            if (rows > 0) {
                message = "Utilizatorul a fost sters cu succes!";
                return 1;
            } else {
                message = "Utilizatorul nu a fost sters!";
                return -1;
            }

        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public int AddCustomer(String username, String password, String name, String surname, String address, String district, String city, String phone, String fax, String email, String postalCode) {
        try {

            String query = "INSERT INTO customers (`Username`,`Password`,`Name`,`Surname`,`Address`,`District`,`City`,`Phone`,`Fax`,`Email`,`postalCode`)"
                    + " VALUES ('" + username + "','" + Utils.md5(password) + "','" + name + "','" + surname + "','" + address + "','" + district + "','" + city + "','" + phone + "','" + fax + "','" + email + "','" + postalCode + "')";

            int rows = db.ExecuteUpdate(query);

            if (rows > 0) {
                message = "Contul a fost creat cu succes.";

                return 1;
            } else {
                message = "Contul nu a fost creat.";
                return -1;
            }

        } catch (Exception ex) {
            return -1;
        } finally {
            //  db.close();
        }
    }

    public String GetMessage() {
        return message;
    }
}
